package main

import (
	"html/template"
	"imooc.com/hello/controller"
	"log"
	"net/http"
)



func RegisterView()  {
	//解析模板(**代表目录，*代表文件)
	tpl, err := template.ParseGlob("view/**/*")
	if err != nil {
		//打印并直接退出
		log.Fatal(err.Error())
	}
	//获取目录下所有的文件
	for _,v := range tpl.Templates(){
		//获取文件名
		tplName := v.Name()
		http.HandleFunc(tplName, func(w http.ResponseWriter, r *http.Request) {
			//执行文件
			tpl.ExecuteTemplate(w, tplName, nil)
		})
	}

}


func main() {
	http.HandleFunc("/user/login", controller.UserLogin)
	http.HandleFunc("/user/register", controller.UserRegister)
	http.HandleFunc("/user/addFriend", controller.AddFriend)
	http.HandleFunc("/user/friends", controller.LoadFriends)
	http.HandleFunc("/chat", controller.Chart)
	http.HandleFunc("/attach/upload", controller.Upload)
	http.HandleFunc("/user/loadCommunitys", controller.LoadCommunitys)
	http.HandleFunc("/user/createCommunity", controller.CreateCommunity)
	http.HandleFunc("/user/joinCommunity", controller.JoinCommunity)
	http.HandleFunc("/user/find", controller.UserFind)
	//提供静态资源目录支持
	//http.Handle("/", http.FileServer(http.Dir("hello/")))
	//为了不爆露main.go的代码,指定其他访问
	http.Handle("/asset/", http.FileServer(http.Dir(".")))
	http.Handle("/media/", http.FileServer(http.Dir(".")))

	//通过 /user/login.shtml 访问login页面
	/*http.HandleFunc("/user/login.shtml", func(w http.ResponseWriter,
		r *http.Request) {
		//解析模板
		tpl, err := template.ParseFiles("hello/view/user/login.html")
		if err != nil {
			//打印并直接退出
			log.Panic(err.Error())
		}
		tpl.ExecuteTemplate(w, "/user/login.shtml", nil)
	})
	//通过 /user/register.shtml 访问login页面
	http.HandleFunc("/user/register.shtml", func(w http.ResponseWriter,
		r *http.Request) {
		//解析模板
		tpl, err := template.ParseFiles("hello/view/user/register.html")
		if err != nil {
			//打印并直接退出
			log.Panic(err.Error())
		}
		tpl.ExecuteTemplate(w, "/user/register.shtml", nil)
	})*/
	RegisterView()
	//启动web服务器
	http.ListenAndServe(":8080", nil)
}
